package com.sq.swing.frame;

import javax.swing.text.*;

public final class LengthRestrictedDocument extends PlainDocument {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private final int limit;

	public LengthRestrictedDocument(int limit) {
		this.limit = limit;
	}

	@Override
	public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
		if (str == null)
			return;

		if ((getLength() + str.length()) <= limit) {
			super.insertString(offs, str, a);
		}
	}
}
